package com.work08.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.work08.common.R;
import com.work08.common.Upload;
import com.work08.pojo.Article;
import com.work08.pojo.Category;
import com.work08.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 绛河
 * @date 2023/6/19
 * @apiNote
 */
@RestController
@RequestMapping("/api/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private Upload upload;

    @PostMapping("/addArticle")
    public R<String> addArticle(MultipartFile picture,Article article) throws FileNotFoundException {
        System.out.println("OK");
        System.out.println(article);
        System.out.println(picture.getOriginalFilename());
        upload.saveFile(picture,"article");
        article.setPhoto("/article/"+picture.getOriginalFilename());
        article.setStatus(0);
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());
        article.setViewCount(0);
        articleService.save(article);
       return R.success("保存成功");
    }

    @GetMapping("/getAllByPass")
    public R<List<Article>> getAllByPass(){
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Article::getStatus,1);
        List<Article> list = articleService.list(lambdaQueryWrapper);
        return R.success(list);
    }

    @GetMapping("/getAll")
    public R<List<Article>> getAll(){
        List<Article> list = articleService.list();
        return R.success(list);
    }

    @GetMapping("/getAllByPage")
    public R<Page> getAllByPage(String title,Integer current,Integer pageSize){
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(title!=null&&title!="",Article::getTitle,title);
        Page page1 = new Page(current,pageSize);
        articleService.page(page1,lambdaQueryWrapper);
        return R.success(page1);

    }
    @PutMapping("/updateByViewCount")
    public R<String> updateByViewCount(Integer id){
        LambdaUpdateWrapper<Article> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Article::getId,id);
        lambdaUpdateWrapper.setSql("view_count = view_count + 1");
        articleService.update(lambdaUpdateWrapper);
        return R.success("增加成功");
    }

    @PutMapping("/audit")
    public R<String> audit(Integer id){
        LambdaUpdateWrapper<Article> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Article::getId,id);
        lambdaUpdateWrapper.set(Article::getStatus,1);
        articleService.update(lambdaUpdateWrapper);
        return R.success("审核成功");
    }

    @DeleteMapping("/deleteById")
    public R<String> deleteById(Integer id){
        LambdaUpdateWrapper<Article> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Article::getId,id);
        articleService.remove(lambdaUpdateWrapper);
        return R.success("删除成功");
    }

    @DeleteMapping("/deleteArticleByBatch")
    public R<String> deleteCategoryByBatch(@RequestBody Integer[] ids){
        LambdaUpdateWrapper<Article> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.in(Article::getId,ids);
        articleService.remove(lambdaUpdateWrapper);
        return R.success("删除成功");
    }
}
